﻿using System;
using System.Windows.Forms;
using System.Xml;
using GMap.NET.EXT;
using LeakView.service;

namespace LeakView.view
{
    public partial class FormServerConfig : Form
    {
        private string pipeline_name;
        public string Pipeline_name
        {
            get { return pipeline_name; }
        }

        private double pipeline_length;
        public double Pipeline_length
        {
            get { return pipeline_length; }
        }

        private double gas_speed;
        public double Gas_speed
        {
            get { return gas_speed; }
        }

        private string coordinates;
        public string Coordinates
        {
            get { return coordinates; }
        }

        public FormServerConfig()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pipeline_name = this.textBox_name.Text;
            pipeline_length = double.Parse(this.textBox_length.Text);
            gas_speed = double.Parse(this.textBox_speed.Text);
            coordinates = this.textBox_points.Text;
            this.DialogResult = DialogResult.OK;

        }

        private void FormServerConfig_Load(object sender, EventArgs e)
        {
            this.textBox_name.Text = MainModule.pair.Value.Value.name;
            //this.textBox_length.Text = ServerInfo.ME.Info.pipeline_length.ToString();
            //this.textBox_speed.Text = AjaxModules.pair.Value.Value.gas_speed.ToString();
            //this.textBox_points.Text = AjaxModules.server.Value.Value.coordinates;
        }

        private void textBox_points_TextChanged(object sender, EventArgs e)
        {
            this.textBox_length.Text = PointLatLngs.parse(this.textBox_points.Text, " ", ",").Distance.ToString();
        }

        private void buttonImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "KML文件(*.kml;*.xml)|*.kml;*.xml|所有文件|*.*";
            ofd.ValidateNames = true;
            ofd.CheckPathExists = true;
            ofd.CheckFileExists = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                XmlDocument xmlDoc = loadXmlFile(ofd.FileName);
                if(xmlDoc != null)
                {
                    XmlNode node = xmlDoc.SelectSingleNode("//coordinates/text()");
                    if (node != null)
                    {
                        this.textBox_points.Text = node.Value;
                    }
                }
            }
        }

        static XmlDocument loadXmlFile(string path)
        {

            XmlDocument oldXmlDoc = new XmlDocument();

            XmlDocument doc;

            try
            {

                // remove XML comment <!-- -->

                XmlReaderSettings settings = new XmlReaderSettings();

                settings.IgnoreComments = true;

                XmlReader reader = XmlReader.Create(path, settings);

                // load old xml doc

                oldXmlDoc.Load(reader);



                // remove XML ns

                string newXml = System.Text.RegularExpressions.Regex.Replace(

                oldXmlDoc.OuterXml,

                @"(xmlns:?[^=]*=[""][^""]*[""])", "",

                System.Text.RegularExpressions.RegexOptions.IgnoreCase |

                System.Text.RegularExpressions.RegexOptions.Multiline);



                // new xml doc to replace old XmlDoc

                doc = new XmlDocument();



                doc.LoadXml(newXml);

            }
            catch (System.IO.DirectoryNotFoundException e)
            {

                Console.WriteLine(e.ToString());

                doc = null;

            }
            catch (XmlException xmle)
            {

                Console.WriteLine(xmle.ToString());

                doc = null;

            }

            return doc;

        }
    }
}
